﻿// hobbies manager usage see myprofile page

var hobbiesManager = new function () {
    var editWithComments = true;
    var labels = {comment: "Comment"};

    this.init = function (enableComments, labelsParams) {
        editWithComments = enableComments;
        if (labelsParams != null)
            labels = labelsParams;
        addFilterDDL("", 0);
        for (var i = 0; i < listHobbies.length; i++)
            printRow(listHobbies[i], i);
        $("#btnAddHobbies").click(function () {
            var selCode = "", selLabel = "", idx = 0;
            while (true) {
                var t = $("#hobbiesSel" + idx);
                if (t.length == 0 || t.val() == "")
                    break;
                idx++;
                selCode = t.val();
                selLabel = t.find("option:selected").text();
            }
            var newEl = { "CodeHobby": selCode, "LabelHobby": selLabel, "Description": "" };
            listHobbies[listHobbies.length] = newEl;
            printRow(newEl, listHobbies.length - 1);
            return false;
        });
    };

    function printRow(el, idx) {
        //hobbiesList
        var r, c;
        r = document.createElement("tr");
        c = document.createElement("td");
        c.innerHTML = "<img class='imgButton deleteHobby' code='" + el.CodeHobby + "' id='deleteHobby_" + el.CodeHobby + "' src='../Images/Delete.png' />";
        r.appendChild(c);
        c = document.createElement("td");
        c.innerHTML = el.LabelHobby + "<input type='hidden' name='user.UsersHobbies[" + idx + "].CodeHobby' class='hobbySelVal' value='" + el.CodeHobby + "'/>";
        r.appendChild(c);
        if (editWithComments) {
            c = document.createElement("td");
            if (el.Description == null)
                el.Description = "";
            c.innerHTML = "<textarea name='user.UsersHobbies[" + idx + "].Description' rows=2  aria-label=\"" + labels.comment + "\" placeholder=\"" + labels.comment + "\"  style='width: 500px; padding'>" + el.Description + "</textarea>";
            r.appendChild(c);
        }
        $("#hobbiesList")[0].appendChild(r);
        $(".deleteHobby").click(delHobby);
    }

    function addFilterDDL(parent, idx) {
        $.ajax({
            type: "POST",
            url: "/Referential/ListHobbies",
            contentType: "application/json; charset=utf-8",
            data: "{'codeParent':\"" + parent + "\"}",
            dataType: "json",
            success: function (result) {
                if (result != null && result.length > 0) {
                    var ddlId = "hobbiesSel" + idx;
                    $("#hobbiesDDL").append(" <select id='" + ddlId + "' idx='" + idx + "' ></select>");
                    var ddl = $("#" + ddlId);
                    ddl.append(new Option("", ""));
                    for (var i = 0; i < result.length; i++) {
                        var d = result[i];
                        ddl.append(new Option(d.Label, d.Code));
                    }
                    ddl.change(function () {
                        var val = $(this).val();
                        var index = parseInt($(this).attr("idx"));
                        if (val == "") {
                            removeIdxAfter(index + 1);
                        } else {
                            removeIdxAfter(index + 1);
                            addFilterDDL(val, index + 1);
                        }
                        return true;
                    });
                }
            },
            error: function (result) {
                debugger;
            }
        });
    };

    function delHobby() {
        var code = $(this).attr("code"), idx = -1, i;
        for (i = 0; i < listHobbies.length; i++) {
            var el = listHobbies[i];
            if (el.CodeHobby == code) {
                idx = i;
                break;
            }
        }
        if (idx >= 0) {
            listHobbies.splice(idx, 1);
            $("#hobbiesList")[0].deleteRow(idx);
            for (i = idx + 1; i < listHobbies.length + 1; i++) {
                $("input[name=\"user.UsersHobbies[" + i + "].CodeHobby\"]").attr("name", "user.UsersHobbies[" + (i - 1) + "].CodeHobby");
                $("textarea[name=\"user.UsersHobbies[" + i + "].Description\"]").attr("name", "user.UsersHobbies[" + (i - 1) + "].Description");
            }

            refreshBtn();
        }
    }

    function removeIdxAfter(idx) {
        while (true) {
            var t = $("#hobbiesSel" + idx);
            if (t.length == 0)
                break;
            t.remove();
            idx++;
        }
    }
};
